package com.item.util;

import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by admin on 2016/12/6.
 */
public class EncryptionUtil {

    private static Logger log = LoggerFactory.getLogger(EncryptionUtil.class);


    /**
     * 封装aeskey,验签,参数加密 (用于httpclient方式调用)
     * @param data
     * @param privateKey
     * @param publicKey
     * @return
     * @throws Exception
     */
    public static Map cipherData(Map data, String privateKey, String publicKey) throws Exception {
        Map urlParam = new HashMap();
        //aes加密  base64转码
        JSONObject jsonObject = (JSONObject) JSONObject.toJSON(data);
        String aesPassword = CharUtil.createRandomStr(16);
        String paramEncode = Base64Util.encode(AesUtil.encrypt(jsonObject.toJSONString(), aesPassword));
        //rsa私钥签名  base64转码
        String signEncode = Base64Util.encode(RsaUtil.sign(jsonObject.toJSONString(),privateKey,"utf-8").getBytes());
        //rsa对方公钥加密 base64转码
        String keyPassEncode = Base64Util.encode(RsaUtil.encrypt(publicKey,aesPassword.getBytes()));
        urlParam.put("data", paramEncode);
        urlParam.put("sign", signEncode);
        urlParam.put("keyPass", keyPassEncode);
        return urlParam;
    }

    /**
     * 解密password 解密data
     * @param data          Aes加密后入参
     * @param keypass       rsa加密后password
     * @param privateKey    Rsa私钥
     * @return
     * @throws Exception
     */
    public static Map unCipherPassAndDate(String data, String keypass, String privateKey) throws Exception {
        Map dataMap = null;
        //解密password
        keypass = new String(RsaUtil.decrypt(privateKey, Base64Util.decode(keypass)));
        //aes解密参数
        String param = new String(AesUtil.decrypt(Base64Util.decode(data),keypass), StandardCharsets.UTF_8);
        dataMap = JSONUtil.toObject(param,HashMap.class);
        dataMap.put("param",param);
        return dataMap;
    }



    /**
     * 入参加密和入参签名
     *
     * @param data       待Aes加密入参
     * @param password   Aes私钥
     * @param privateKey Rsa私钥
     * @return
     */
    public static Map cipherData(String data, String password, String privateKey) {
        Map map = new HashMap();
        //aes加密参数数据  base64转码
        String encryptData = Base64Util.encode(AesUtil.encrypt(data, password));
        //rsa私钥签名  base64转码
        String signData = Base64Util.encode(RsaUtil.sign(data, privateKey,"utf-8").getBytes());
        map.put("data", encryptData);
        map.put("sign", signData);
        return map;
    }
    /**
     * aes解密
     * @param data
     * @param password
     * @return
     * @throws Exception
     */
    public static String unCipherDate(String data, String password) throws Exception {
        Map dataMap = null;
        //aes解密参数
        String decryptedData = new String(AesUtil.decrypt(Base64Util.decode(data), password), "utf-8");
        return decryptedData;
    }


    /**
     * 验签
     * @param data
     * @param sign
     * @param publicKey
     * @return
     */
    public static boolean verify(String data, String sign, String publicKey){
        //base64转码
        String signDecode = new String(Base64Util.decode(sign));
        //验证来源
        boolean verify = RsaUtil.verify(data,signDecode,publicKey,"utf-8");
        return verify;
    }
}
